package com.dp.guardedsuspension;

import java.util.LinkedList;

/**
 * 请求队列中包含了队列的存储结构和对队列的操作方法
 * @author home
 */
public class RequestQueue {
    private LinkedList<Request> queue = new LinkedList<Request>();

    public synchronized Request getRequest() {
        while (queue.size() == 0) {
            try {
                wait();// 等待直到有新的Request加入
            } catch (InterruptedException e) {
            }
        }
        System.out.println("getRequest RequestQueue's size is " + queue.size());
        return queue.remove(); // 返回队列中第一个请求
    }

    public synchronized void addRequest(Request request) {
        queue.add(request);
        System.out.println("addRequet RequestQueue's size is " + queue.size());
        notifyAll(); // 通知GetRequest()方法
    }

}
